package com.example.Sao.MyActivity;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.Sao.Data.BeanData;
import com.example.Sao.Data.MListAdapter;
import com.example.Sao.Data.MSpinnerAdapter;
import com.example.Sao.R;
import com.example.Sao.Serivce.SerialPortService;

public class SymbologiesQR extends MyBaseActivity {
	
	private final String Tag = "SymbologiesQR";
	
	String Postal2DOptions[] = { 
			          "2D Postal Codes Off", "Australian Post On", "British Post On", "Canadian Post On", 
			          "Intelligent Mail Bar Code On", "Japanese Post On", "KIX Post On", "Planet Code On",              
			          "Postal-4i On",  "Postnet On",  "Postnet with B and B’ Fields On", "InfoMail On",       
			          "InfoMail and British Post On", "Intelligent Mail Bar Code and Postnet with B and B’ Fields On", 
			          "Postnet and Postal-4i On", "Postnet and Intelligent Mail Bar Code On",
			          "Postal-4i and Intelligent Mail Bar Code On",  "Postal-4i and Postnet with B and B’ Fields On",  
			          "Planet Code and Postnet On", "Planet Code and Postnet with B and B’ Fields On",
			          "Planet Code and Postal-4i On", "Planet Code and Intelligent Mail Bar Code On", 
			          "Planet Code, Postnet, and Postal-4i On",  "Planet Code, Postnet, and Intelligent Mail Bar Code On",  
			          "Planet Code, Postal-4i, and Intelligent Mail Bar Code On",   "Postnet, Postal-4i, and Intelligent Mail Bar Code On", 
			          "Planet Code, Postal-4i, and Postnet with B and B’ Fields On",  "Planet Code, Intelligent Mail Bar Code, and Postnet with B and B’ Fields On",
			          "Postal-4i, Intelligent Mail Bar Code, and Postnet with B and B’ Fields On", "Planet Code,Postal-4i,Intelligent Mail Bar Code, and Postnet On",   
			          "Planet Code,Postal-4i,Intelligent Mail Bar Code, and Postnet with B and B’ Fields On"
			};
	
	String Postal2DCommands[] = {
			           "postal0.",  "postal1.",  "postal7.",   "postal30.",
			           "postal10.", "postal3.",  "postal4.",   "postal5.",   
			           "postal9.",  "postal6.",  "postal11.",  "postal2.",
			           "postal8.",  "postal20.", 
			           "postal14.", "postal16.",
			           "postal17.", "postal19.", 
			           "postal12.", "postal18.",
			           "postal13.", "postal15.", 
			           "postal21.", "postal22.",
			           "postal23.", "postal24.",
			           "postal25.", "postal26.", 
			           "postal27.", "postal28.", 
			           "postal29."
	       };


    public  List<BeanData> mListData;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.symbologies_qr);
		
		MyApplication  mApp  = (MyApplication)getApplication();
		mListData = mApp.initQRData();
		initListViews();
		initSpinnerViews();	
	}
	
	
	private void initListViews(){
		
		
		    ListView  mListView = (ListView)findViewById(R.id.mList);
	        final MListAdapter mMListAdapter = new MListAdapter(this, mListData);
	        mListView.setAdapter(mMListAdapter);
	        mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					//Log.e(Tag, "onItemClick: position = " + position);
					BeanData mBeanData = (BeanData)mMListAdapter.getItem(position);
					
					if(mBeanData.isOpened()){
						mBeanData.setOpened(false);
						mListData.get(position).setOpened(false);
						
					}else{
						mBeanData.setOpened(true);
						mListData.get(position).setOpened(true);
					}
					
			  		SharedPreferences mSP = getSharedPreferences(MyApplication.SYMBOL_CONFIG_QR, 0);
			  		SharedPreferences.Editor editor = mSP.edit();
			  	    editor.putBoolean(mListData.get(position).getmSymbName(), mListData.get(position).isOpened() ? true : false);
			  		editor.commit();
					
					String command  = mListData.get(position).getmSubCommand();

					if(!mListData.get(position).isOpened()){
						command = command.substring(0, command.length()-2) + "0.";
					}
					
					//command = MyApplication.FACTORY_DEFAULT;
					
					Log.e(Tag, "command = " + command);
					
					Intent intent = new Intent(SerialPortService.BARCODEPORT_WRITEDATA_ACTION);
					intent.putExtra(SerialPortService.BARCODEPORT_WRITEDATA_EXTRA_DATA, command);
					SymbologiesQR.this.sendBroadcast(intent);
					
					if(mListData.get(position).isOpened()){
						Toast.makeText(SymbologiesQR.this,"open " + "  " + mListData.get(position).getmSymbName(), Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(SymbologiesQR.this,"close" + "  " + mListData.get(position).getmSymbName(), Toast.LENGTH_SHORT).show();
					}

					mMListAdapter.notifyDataSetChanged();
				}
			});
	}
	
	boolean mFlag = false;
	private void initSpinnerViews(){

		MSpinnerAdapter  mAdapter =  new MSpinnerAdapter(this, Postal2DOptions);
		Spinner mSpinnerPostal2D = (Spinner)findViewById(R.id.mPostalCodes2D);
		mSpinnerPostal2D.setAdapter(mAdapter);
		
  		SharedPreferences mSP = getSharedPreferences(MyApplication.SYMBOL_CONFIG_QR, 0);
  		int mIndex = mSP.getInt(MyApplication.KEY_POSTAL_2D, 0);

  		
		mSpinnerPostal2D.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

               	String command  = Postal2DCommands[position];  
               	
                Log.e(Tag, "onItemSelected:  position = " + position + "; command = " + command);
                
                if(!mFlag){
                	mFlag = true;
                	return;
                }
                
		  		SharedPreferences mSP = getSharedPreferences(MyApplication.SYMBOL_CONFIG_QR, 0);
		  		SharedPreferences.Editor editor = mSP.edit();
		  	    editor.putInt(MyApplication.KEY_POSTAL_2D, position);
		  		editor.commit();
               	
   				Intent intent = new Intent(SerialPortService.BARCODEPORT_WRITEDATA_ACTION);
   				intent.putExtra(SerialPortService.BARCODEPORT_WRITEDATA_EXTRA_DATA, command);
   				sendBroadcast(intent);
   			    Toast.makeText(SymbologiesQR.this, "" + Postal2DOptions[position], Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
		
  		mSpinnerPostal2D.setSelection(mIndex);

	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MyApplication.onSettings = true;
	}


	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MyApplication.onSettings = false;
	}


}
